package com.suray.wcs.service.system.service.impl;

import com.suray.wcs.service.system.entity.WareHouseDB;
import com.suray.wcs.service.system.service.IWareHouseDBService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * @author caox@suray.cn
 * @description:获取wmsIpAndPort
 * @date 2020/7/25 11:57
 */
@Component
public class WmsIpPortUtil {

    @Autowired
    private IWareHouseDBService IWareHouseDBService;

    public static WmsIpPortUtil wmsIpPortUtil;

    public static final String COLON_CONNECTOR = ":";

    public static final String HTTP_CONNECTOR = "http://";

    @PostConstruct
    public void init() {
        wmsIpPortUtil = this;
    }

    public static String getWmsIp() {
        WareHouseDB wareHouseDB = getWcsDistrict();
        if (wareHouseDB != null) {
            return wareHouseDB.getWmsIp();
        }
        return StringUtils.EMPTY;
    }

    public static String getWmsPort() {
        WareHouseDB wareHouseDB = getWcsDistrict();
        if (wareHouseDB != null) {
            return wareHouseDB.getWmsPort();
        }
        return StringUtils.EMPTY;
    }

    public String getWmsIp(String districtName) {
        WareHouseDB wareHouseDB = IWareHouseDBService.getCurWareHouseDB();
        if (wareHouseDB != null) {
            return wareHouseDB.getWmsIp();
        }
        return StringUtils.EMPTY;
    }


    public String getWmsPort(String districtName) {
        WareHouseDB wareHouseDB = IWareHouseDBService.getCurWareHouseDB();
        if (wareHouseDB != null) {
            return wareHouseDB.getWmsPort();
        }
        return StringUtils.EMPTY;
    }

    public static String getUrl(String contextPath) {
        return new StringBuilder().append(HTTP_CONNECTOR).append(getWmsIp()).append(COLON_CONNECTOR).append(getWmsPort()).append(contextPath).toString();
    }


    private static WareHouseDB getWcsDistrict() {
        WareHouseDB wareHouseDB = null;
        List<WareHouseDB> wareHouseDBList = wmsIpPortUtil.IWareHouseDBService.queryAllWcsDist();
        if (CollectionUtils.isNotEmpty(wareHouseDBList)) {
            wareHouseDB = wareHouseDBList.get(0);
        }
        return wareHouseDB;
    }

}
